#include <QByteArray>

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QSettings *set, QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::MainWindow), m_set(set)
{
    m_ui->setupUi(this);
    m_defTitle = windowTitle();
    connect(m_ui->textBrowser, SIGNAL(status(QString)), m_ui->statusEdit, SLOT(setText(QString)));
}

MainWindow::~MainWindow()
{
    delete m_ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::closeEvent(QCloseEvent *e)
{
    if(!isHidden())
    {
        QByteArray hSplitterState = hSplitter()->saveState();
        QByteArray vSplitterState = vSplitter()->saveState();
        QByteArray window = saveGeometry();

        m_set->setValue("hSplitter", hSplitterState);
        m_set->setValue("vSplitter", vSplitterState);
        m_set->setValue("window", window);

        m_set->sync();
    }
    debug("close()");

    e->accept();

    //emit closed();
}

CTextBrowser* MainWindow::textBrowser()
{
    return m_ui->textBrowser;
}

CTableView* MainWindow::tableView()
{
    return m_ui->tableView;
}

CTreeView* MainWindow::treeView()
{
    return m_ui->treeView;
}

QSplitter* MainWindow::hSplitter()
{
    return m_ui->horizontalSplitter;
}

QSplitter* MainWindow::vSplitter()
{
    return m_ui->verticalSplitter;
}

void MainWindow::show()
{
    if(m_set->value("hSplitter").isValid())
        hSplitter()->restoreState(m_set->value("hSplitter").toByteArray());

    if(m_set->value("vSplitter").isValid())
        vSplitter()->restoreState(m_set->value("vSplitter").toByteArray());

    if(m_set->value("window").isValid())
        restoreGeometry(m_set->value("window").toByteArray());

    QMainWindow::show();
}

void MainWindow::setTitle(QString title)
{
    setWindowTitle(m_defTitle + ": " + title);
}
